package demo1;

import java.util.Stack;

class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minstack;

    public MinStack() {
        this.stack = new Stack<>();
        this.minstack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if(minstack.empty()){
            minstack.push(val);
        }else if(val <= minstack.peek()){
            minstack.push(val);
        }
    }
    
    public void pop() {
      if(stack.empty()){
          return;
      }
      int num = stack.pop();
      if(num == minstack.peek()){
          minstack.pop();
      }
    }
    
    public int top() {
        if(stack.empty()){
            return -1;
        }
        return stack.peek();
    }
    
    public int getMin() {
        if(minstack.empty()){
            return -1;
        }
        return minstack.peek();
    }
}